<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>围棋收集游戏</title>
    <style>
        /* 基础样式重置 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        /* 游戏容器布局 */
        .game-container {
            padding: 10px;
            min-height: 100vh;
        }

        .controls {
            margin-bottom: 20px;
            text-align: center;
        }

        .controls button {
            margin: 0 10px;
            padding: 8px 16px;
            font-size: 16px;
            cursor: pointer;
        }

        .score,
        .timer {
            display: inline-block;
            margin: 0 15px;
            font-size: 18px;
        }

        /* 修改游戏区域布局 */
        .game-area {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 20px;
            width: 100%;
        }

        .go-board {
            width: 450px;
            height: 450px;
            background-color: #DEB887;
            position: relative;
            border: 2px solid #000;
            box-sizing: border-box;
            padding: 45px;
        }

        .intersection {
            position: absolute;
            width: 30px;
            height: 30px;
        }

        .go-board::before,
        .go-board::after {
            content: '';
            position: absolute;
            top: 45px;
            /* 与padding相同 */
            left: 45px;
            /* 与padding相同 */
            right: 45px;
            /* 与padding相同 */
            bottom: 45px;
            /* 与padding相同 */
            background-image: repeating-linear-gradient(to bottom,
                    #000 0px,
                    #000 1px,
                    transparent 1px,
                    transparent 40px),
                linear-gradient(to bottom,
                    transparent 0,
                    transparent calc(100% - 1px),
                    #000 calc(100% - 1px),
                    #000 100%);
        }

        .go-board::after {
            background-image: repeating-linear-gradient(to right,
                    #000 0px,
                    #000 1px,
                    transparent 1px,
                    transparent 40px),
                linear-gradient(to right,
                    transparent 0,
                    transparent calc(100% - 1px),
                    #000 calc(100% - 1px),
                    #000 100%);
        }

        /* PC端棋子样式 */
        .stone {
            position: absolute;
            width: 40px;
            height: 40px;
            background-color: black;
            border-radius: 50%;
            cursor: move;
            z-index: 2;
            transform: translate(-50%, -50%);
            box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
        }

        .stone-plate {
            width: 150px;
            height: 150px;
            background-color: #8B4513;
            border-radius: 10px;
            margin-left: 20px;
            position: relative;
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            grid-template-rows: repeat(3, 1fr);
            padding: 12px;
            gap: 6px;
            box-sizing: border-box;
        }

        .collected-stone {
            width: 40px;
            height: 40px;
            background-color: black;
            border-radius: 50%;
            margin: auto;
            box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
        }

        .floor {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            height: 50px;
            background-color: #8B4513;
            z-index: -1;
        }

        /* 修改游戏说明样式 */
        .game-instructions {
            padding: 5px;
            background-color: #f5f5f5;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            position: sticky;
            top: 10px;
            margin-bottom: 10px;
        }

        .game-instructions h2 {
            color: #333;
            margin-bottom: 15px;
            text-align: center;
            font-size: 1.2em;
        }

        .game-instructions ul {
            padding-left: 10px;
            line-height: 1.2;
        }

        .game-instructions li {
            margin-bottom: 5px;
            color: #555;
            font-size: 0.95em;
        }

        /* 右侧区域样式 */
        .right-area {
            display: flex;
            justify-content: center;
            gap: 20px;
            width: 100%;
        }

        /* 修改按钮样式 */
        #startBtn {
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            transition: background-color 0.3s;
        }

        #startBtn:hover {
            background-color: #45a049;
        }

        #startBtn:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }

        /* 修改掉落棋子的样式 */
        .dropped-stone {
            position: absolute;
            width: 26px;
            height: 26px;
            background-color: black;
            border-radius: 50%;
            box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
            left: 50%;
            bottom: 0;
            /* 直接放在底部 */
            transform: translateX(-50%);
            /* 水平居中 */
        }

        /* 禁用按钮样式 */
        button:disabled {
            opacity: 0.6;
            cursor: not-allowed;
        }

        /* 响应式布局 */
        @media screen and (max-width: 768px) {
            .game-layout {
                flex-direction: column;
            }

            .game-instructions {
                width: 100%;
                position: static;
                margin-bottom: 15px;
            }

            .game-right {
                width: 100%;
            }

            .controls {
                flex-wrap: wrap;
                gap: 10px;
            }

            #startBtn {
                width: 100%;
                margin-bottom: 10px;
            }

            .score,
            .timer {
                font-size: 16px;
                margin: 5px;
            }
        }

        /* 移动端棋盘适配 */
        @media screen and (max-width: 600px) {
            .game-container {
                padding: 5px;
            }

            .game-area {
                gap: 15px;
                width: 100%;
                padding: 0 5px;
                /* 添加水平内边距 */
            }

            .go-board {
                width: 288px;
                height: 288px;
                padding: 16px;
            }

            .go-board::before,
            .go-board::after {
                content: '';
                position: absolute;
                top: 16px;
                left: 16px;
                right: 16px;
                bottom: 16px;
                background-image: repeating-linear-gradient(to bottom,
                        #000 0px,
                        #000 1px,
                        transparent 1px,
                        transparent 32px),
                    linear-gradient(to bottom,
                        transparent 0,
                        transparent calc(100% - 1px),
                        #000 calc(100% - 1px),
                        #000 100%);
            }

            .go-board::after {
                background-image: repeating-linear-gradient(to right,
                        #000 0px,
                        #000 1px,
                        transparent 1px,
                        transparent 32px),
                    linear-gradient(to right,
                        transparent 0,
                        transparent calc(100% - 1px),
                        #000 calc(100% - 1px),
                        #000 100%);
            }

            /* 调整右侧区域布局 */
            .right-area {
                gap: 10px;
                justify-content: center;
                align-items: center;
                margin-bottom: 20px;
                /* 添加底部边距 */
            }

            /* 调整盘子大小以适应更大的棋子 */
            .stone-plate {
                width: 120px;
                /* 从80px增加到120px */
                height: 120px;
                padding: 8px;
                gap: 4px;
            }

            .stone {
                width: 26px;
                /* 从13px增加到26px */
                height: 26px;
            }

            .collected-stone {
                width: 26px;
                /* 从13px增加到26px */
                height: 26px;
            }
        }

        /* 针对更小屏幕的优化 */
        @media screen and (max-width: 360px) {
            .go-board {
                width: 288px;
                /* 19格 * 14px + 32px边距 */
                height: 288px;
                padding: 16px;
            }

            .go-board::before,
            .go-board::after {
                top: 16px;
                left: 16px;
                right: 16px;
                bottom: 16px;
                background-image: repeating-linear-gradient(to bottom,
                        transparent 0,
                        transparent 13px,
                        #000 13px,
                        #000 14px);
            }

            .go-board::after {
                background-image: repeating-linear-gradient(to right,
                        transparent 0,
                        transparent 13px,
                        #000 13px,
                        #000 14px);
            }

            .stone {
                width: 12px;
                height: 12px;
            }

            .collected-stone {
                width: 12px;
                height: 12px;
            }
        }

        /* 触摸设备的拖拽优化 */
        @media (hover: none) {
            .stone {
                touch-action: none;
                -webkit-user-select: none;
                user-select: none;
            }

            .stone:active {
                opacity: 0.7;
            }
        }

        /* 防止iOS上的橡皮筋效果 */
        body {
            overscroll-behavior: none;
            -webkit-overflow-scrolling: touch;
        }

        /* 优化移动端按钮点击体验 */
        #startBtn {
            -webkit-tap-highlight-color: transparent;
            touch-action: manipulation;
        }

        /* 添加弹窗样式 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 1000;
            justify-content: center;
            align-items: center;
        }

        .modal-content {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            text-align: center;
            max-width: 300px;
            width: 90%;
        }

        .modal-content p {
            margin-bottom: 20px;
            font-size: 16px;
            line-height: 1.4;
        }

        .modal-content button {
            padding: 8px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .modal-content button:hover {
            background-color: #45a049;
        }
    </style>
</head>

<body>
    <div class="game-container">
        <div class="game-layout">
            <!-- 左侧游戏说明 -->
            <div class="game-instructions">
                <h2>游戏玩法说明</h2>
                <ul>
                    <li>点击开始游戏后，棋盘上会随机出现一个黑色棋子</li>
                    <li>在倒计时内将棋子拖动到下方的棋盘中收集</li>
                    <li>成功放入记1分，总共需要收集9个棋子</li>
                    <li>如果超时或棋子掉到棋盘外，游戏失败</li>
                </ul>
            </div>

            <!-- 右侧游戏区域 -->
            <div class="game-right">
                <div class="controls">
                    <button id="startBtn">开始游戏</button>
                    <div class="score">得分: <span id="scoreValue">0</span></div>
                    <div class="timer">倒计时: <span id="timerValue">3.0</span>秒</div>
                </div>

                <div class="game-area">
                    <div id="goBoard" class="go-board"></div>
                    <div class="right-area">
                        <div id="stonePlate" class="stone-plate"></div>
                        <div id="droppedStone" class="dropped-stone-area"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="floor"></div>

    <!-- 在body中添加弹窗结构 -->
    <div id="gameOverModal" class="modal">
        <div class="modal-content">
            <p id="gameOverMessage"></p>
            <button id="closeModal">确定</button>
        </div>
    </div>

    <script>
        let score = 0;
        let timer = null;
        let currentStone = null;
        let isPlaying = false;
        let remainingTime = 3.0;  // 初始倒计时设为3秒
        let collectedStones = 0;
        const MAX_STONES = 9; // 盘子最多放9个棋子

        // 获取当前应该的倒计时时间
        function getCountdownTime() {
            if (collectedStones < 3) {
                return 3.0;  // 前3个棋子3秒
            } else if (collectedStones < 6) {
                return 2.0;  // 3-6个棋子2秒
            } else {
                return 1.5;  // 最后3个棋子1.5秒
            }
        }

        // 初始化棋盘
        const goBoard = document.getElementById('goBoard');
        for (let i = 0; i < 9; i++) {
            for (let j = 0; j < 9; j++) {
                const intersection = document.createElement('div');
                intersection.className = 'intersection';
                goBoard.appendChild(intersection);
            }
        }

        // 创建新棋子
        function createStone() {
            if (currentStone) {
                currentStone.remove();
            }
            currentStone = document.createElement('div');
            currentStone.className = 'stone';
            currentStone.draggable = true;

            // 根据屏幕宽度调整位置计算
            const isMobile = window.innerWidth <= 600;

            // 根据屏幕大小选择合适的尺寸
            let gridSize, boardPadding;
            if (isMobile) {
                gridSize = 32;          // 移动端每格32px
                boardPadding = 16;      // 移动端内边距16px
            } else {
                gridSize = 40;          // PC端每格40px
                boardPadding = 45;      // PC端内边距45px
            }

            // 随机选择一个有效的位置（0-8范围内）
            const row = Math.floor(Math.random() * 9);
            const col = Math.floor(Math.random() * 9);

            // 计算棋子位置，确保在交叉点上
            const x = boardPadding + (col * gridSize);
            const y = boardPadding + (row * gridSize);

            // 设置棋子位置
            currentStone.style.left = x + 'px';
            currentStone.style.top = y + 'px';

            goBoard.appendChild(currentStone);
            currentStone.addEventListener('dragstart', handleDragStart);
            remainingTime = getCountdownTime();  // 根据已收集棋子数设置倒计时
            updateTimer();
        }

        function handleDragStart(e) {
            e.dataTransfer.setData('text/plain', 'stone');
        }

        // 计时器更新
        function updateTimer() {
            document.getElementById('timerValue').textContent =
                Math.max(0, remainingTime).toFixed(1);
        }

        // 添加显示弹窗的函数
        function showGameOverModal(message) {
            const modal = document.getElementById('gameOverModal');
            const messageElement = document.getElementById('gameOverMessage');
            messageElement.textContent = message;
            modal.style.display = 'flex';
        }

        // 添加关闭弹窗的事件处理
        document.getElementById('closeModal').addEventListener('click', () => {
            document.getElementById('gameOverModal').style.display = 'none';
        });

        // 修改游戏循环中的结束处理
        function gameLoop() {
            if (isPlaying) {
                remainingTime -= 0.1;
                updateTimer();

                if (remainingTime <= 0) {
                    showGameOverModal('时间到！你输了！');
                    stopGame();
                }
            }
        }

        // 开始游戏
        document.getElementById('startBtn').addEventListener('click', () => {
            if (!isPlaying) {
                isPlaying = true;
                score = 0;
                collectedStones = 0;
                stonePlate.innerHTML = '';
                document.getElementById('scoreValue').textContent = score;
                document.getElementById('startBtn').disabled = true;
                document.getElementById('droppedStone').innerHTML = '';
                createStone();
                timer = setInterval(gameLoop, 100);
            }
        });

        // 盘子放置区域处理
        const stonePlate = document.getElementById('stonePlate');
        stonePlate.addEventListener('dragover', (e) => {
            e.preventDefault();
        });

        // 修改drop事件处理
        stonePlate.addEventListener('drop', (e) => {
            e.preventDefault();
            const rect = stonePlate.getBoundingClientRect();
            const x = e.clientX - rect.left;
            const y = e.clientY - rect.top;

            if (x >= 0 && x <= rect.width && y >= 0 && y <= rect.height) {
                if (isPlaying) {
                    if (collectedStones < MAX_STONES) {
                        const collectedStone = document.createElement('div');
                        collectedStone.className = 'collected-stone';
                        stonePlate.appendChild(collectedStone);

                        collectedStones++;
                        score += 1;
                        document.getElementById('scoreValue').textContent = score;
                        createStone();  // 这里会自动设置新的倒计时
                    } else {
                        showGameOverModal('盘子满了，棋子掉到地上了，你输了！');
                        stopGame(false);
                    }
                }
            } else {
                showGameOverModal('棋子掉到地上了，你输了！');
                stopGame(true);
            }
        });

        function stopGame(clearPlate = true) {
            isPlaying = false;
            if (currentStone) {
                currentStone.remove();
                currentStone = null;
            }
            clearInterval(timer);
            document.getElementById('startBtn').disabled = false;

            if (clearPlate) {
                stonePlate.innerHTML = '';
                collectedStones = 0;
            }
        }

        // 在script标签内添加触摸事件支持
        function initTouchEvents() {
            let draggedStone = null;
            let touchOffset = { x: 0, y: 0 };

            document.addEventListener('touchstart', (e) => {
                const touch = e.touches[0];
                const stone = touch.target.closest('.stone');
                if (stone) {
                    e.preventDefault();
                    draggedStone = stone;
                    const rect = stone.getBoundingClientRect();
                    touchOffset.x = touch.clientX - rect.left;
                    touchOffset.y = touch.clientY - rect.top;
                    stone.style.opacity = '0.7';
                }
            }, { passive: false });

            document.addEventListener('touchmove', (e) => {
                if (draggedStone) {
                    e.preventDefault();
                    const touch = e.touches[0];
                    draggedStone.style.position = 'fixed';
                    draggedStone.style.left = (touch.clientX - touchOffset.x) + 'px';
                    draggedStone.style.top = (touch.clientY - touchOffset.y) + 'px';
                }
            }, { passive: false });

            document.addEventListener('touchend', (e) => {
                if (draggedStone) {
                    const touch = e.changedTouches[0];
                    const plate = document.getElementById('stonePlate');
                    const plateRect = plate.getBoundingClientRect();

                    if (touch.clientX >= plateRect.left &&
                        touch.clientX <= plateRect.right &&
                        touch.clientY >= plateRect.top &&
                        touch.clientY <= plateRect.bottom) {
                        // 成功放入盘子
                        if (isPlaying && collectedStones < MAX_STONES) {
                            const collectedStone = document.createElement('div');
                            collectedStone.className = 'collected-stone';
                            plate.appendChild(collectedStone);
                            collectedStones++;
                            score += 1;
                            document.getElementById('scoreValue').textContent = score;
                            createStone();
                        } else if (collectedStones >= MAX_STONES) {
                            showGameOverModal('盘子满了，棋子掉到地上了，你输了！');
                            stopGame(false);
                        }
                    } else {
                        // 棋子掉落
                        showGameOverModal('棋子掉到地上了，你输了！');
                        stopGame(true);
                    }

                    draggedStone.remove();
                    draggedStone = null;
                }
            });
        }

        // 初始化触摸事件
        initTouchEvents();
    </script>
</body>

</html>